<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/%E6%AD%A6%E6%B1%8932x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/%E6%AD%A6%E6%B1%8916x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib1/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib1/pace/pace-theme-center-simple.min.css">
  <script src="/lib1/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="项目背景存量接口配置旧代码使用Angular框架实现，后续需使用React框架对代码进行重构，为确保重构后代码能够实现原代码功能，而并不造成意外的bug，需对重构后代码书写测试代码以确保其功能的实现。 需求分析前端测试主要分为 3 种：单元测试（Unit Test）、集成测试（Integration Test）、UI 测试（UI Test）。 单元测试是最容易实现的：代码中多个组件共用的工具类库、">
<meta property="og:type" content="article">
<meta property="og:title" content="Jest测试（待整理）">
<meta property="og:url" content="http://example.com/2021/08/15/%E5%AD%A6%E6%A0%A1%E9%A1%B9%E7%9B%AE%E5%BA%94%E7%94%A8/jest%E6%B5%8B%E8%AF%95/index.html">
<meta property="og:site_name" content="Technological Blog">
<meta property="og:description" content="项目背景存量接口配置旧代码使用Angular框架实现，后续需使用React框架对代码进行重构，为确保重构后代码能够实现原代码功能，而并不造成意外的bug，需对重构后代码书写测试代码以确保其功能的实现。 需求分析前端测试主要分为 3 种：单元测试（Unit Test）、集成测试（Integration Test）、UI 测试（UI Test）。 单元测试是最容易实现的：代码中多个组件共用的工具类库、">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-08-15T13:28:16.000Z">
<meta property="article:modified_time" content="2021-11-03T08:32:46.227Z">
<meta property="article:author" content="Li Yudong">
<meta property="article:tag" content="学校项目应用">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://example.com/2021/08/15/%E5%AD%A6%E6%A0%A1%E9%A1%B9%E7%9B%AE%E5%BA%94%E7%94%A8/jest%E6%B5%8B%E8%AF%95/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Jest测试（待整理） | Technological Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Technological Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">IT小白的成长之旅</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="tags fa-fw"></i>标签<span class="badge">18</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="th fa-fw"></i>分类<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="archive fa-fw"></i>归档<span class="badge">95</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/tiarmor1" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/08/15/%E5%AD%A6%E6%A0%A1%E9%A1%B9%E7%9B%AE%E5%BA%94%E7%94%A8/jest%E6%B5%8B%E8%AF%95/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/author.jpg">
      <meta itemprop="name" content="Li Yudong">
      <meta itemprop="description" content="">
    </span>
    
    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Technological Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Jest测试（待整理）
        </h1>
    
        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
    
              <time title="创建时间：2021-08-15 21:28:16" itemprop="dateCreated datePublished" datetime="2021-08-15T21:28:16+08:00">2021-08-15</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-11-03 16:32:46" itemprop="dateModified" datetime="2021-11-03T16:32:46+08:00">2021-11-03</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%AD%A6%E6%A0%A1%E9%A1%B9%E7%9B%AE%E5%BA%94%E7%94%A8/" itemprop="url" rel="index"><span itemprop="name">学校项目应用</span></a>
                </span>
            </span>
    
          
    
        </div>
      </header>
    
    
    
    
    <div class="post-body" itemprop="articleBody">
    
      
        <h2 id="项目背景"><a href="#项目背景" class="headerlink" title="项目背景"></a>项目背景</h2><p>存量接口配置旧代码使用Angular框架实现，后续需使用React框架对代码进行重构，为确保重构后代码能够实现原代码功能，而并不造成意外的bug，需对重构后代码书写测试代码以确保其功能的实现。</p>
<h2 id="需求分析"><a href="#需求分析" class="headerlink" title="需求分析"></a>需求分析</h2><p>前端测试主要分为 3 种：单元测试（Unit Test）、集成测试（Integration Test）、UI 测试（UI Test）。</p>
<p>单元测试是最容易实现的：代码中多个组件共用的工具类库、多个组件共用的子组件等。通常情况下，在公共函数/组件中一定要有单元测试来保证代码能够正常工作。单元测试也应该是项目中数量最多、覆盖率最高的。</p>
<p>集成测试通常被应用在：耦合度较高的函数/组件、经过二次封装的函数/组件、多个函数/组件组合而成的函数/组件等。</p>
<p>UI测试更贴近于我们的开发流程。在前后端分离的开发模式中，前端开发通常会使用到 Mock 的服务器和数据。因而我们需要在开发基本完成后进行相应的 UI 测试。UI 测试的自动化程度还不高，大多数还依赖于手工测试。在一些自动化测试工具中有创建快照的功能，也能帮助我们在一定程度上实现 UI 测试的自动化。</p>
<p>针对重构后部分React代码的功能测试，主要是为了确保其单元组件功能性以及数据传输与显示逻辑的正确，因此更重要的在于其各个组件的单元测试以及各组件本身的UI测试，首要任务是对重构后React各模块的单元测试任务，同时需进行单体的UI测试。</p>
<p>由于项目并未整个重构完成，使用整体集成测试过于复杂且无必要，未重构代码在之前使用中经过多次测试，可以看作稳定的整体，因此其数据传输的格式与方法固定，在理清传输方式后，在外层模拟未重构代码向当前已重构模块进行的数据传输方式，便能够构建出重构模块应有的上下文环境，便能对重构模块整体进行集成测试。</p>
<h2 id="实践步骤"><a href="#实践步骤" class="headerlink" title="实践步骤"></a>实践步骤</h2><h3 id="1、-测试框架选用与依赖配置"><a href="#1、-测试框架选用与依赖配置" class="headerlink" title="1、 测试框架选用与依赖配置"></a>1、 测试框架选用与依赖配置</h3><p>​    Jest是Facebook开源的一个单元测试框架，也是现在应用最广泛的。由于项目使用React hooks实现，因此常用于类组件的UI测试ReactTestUtils不再适用，我们用React hooks testing library来代替，这是一个专门用于测试React hook的库。</p>
<p>项目使用TypeScript编写的TSX代码来实现，在打包运行时会有webpack进行代码的转译。因此测试过程中同样需要添加各类代码转译模块。</p>
<p>1、在jest运行时的全局变量中注入ts-jest，调用与webpack一样的ts.config配置文件，实现s对tsx文件的转译。后续在配置中，使用正则匹配tsx文件，使其调用ts-jest即可。</p>
<p>2、在用ts-jest转译时，由于某些原因，可能无法识别import，这时需要额外配置babel-jest完成从ES6+到ES5的转译。</p>
<p>3、对于.scss、.css 格式文件，配置时使用 jest-css-modules处理，处理方式同上</p>
<h3 id="2、-Redux与React-Intl模拟"><a href="#2、-Redux与React-Intl模拟" class="headerlink" title="2、 Redux与React-Intl模拟"></a>2、 Redux与React-Intl模拟</h3><p>1、该项目使用Redux实现组件间数据的传输与交互，因此在单元测试之前，我们需要先模拟一层Redux的Provider，包裹在组件的外层，来向被测组件传输数据。</p>
<p>2、项目使用React-Intl来进行一个。国际化语言的分类处理，因此我们同样需要在模拟一层IntlProvider，包裹在组件外层</p>
<p>引入Redux和React-Intl后，利用两层Provider完善上下文环境的配置，这时候我们就可以开始进行单元测试的编写了。</p>
<p> 项目采用React-hooks来编写，虽然hook是一个函数，但实际运行会涉及到很多React运行时的东西，因此我们不能用测试普通函数的方法来测试它们，需利用React hooks testing library库来进行测试。</p>
<h3 id="3、-单元测试与UI快照生成"><a href="#3、-单元测试与UI快照生成" class="headerlink" title="3、 单元测试与UI快照生成"></a>3、 单元测试与UI快照生成</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//jest配置</span></span><br><span class="line">  <span class="keyword">let</span> ans = &#123;</span><br><span class="line">    <span class="comment">// 测试用例运行在一个类似于浏览器的环境里，可以调用浏览器的 API</span></span><br><span class="line">    testEnvironment: <span class="string">'jest-environment-jsdom-fourteen'</span>,</span><br><span class="line">    <span class="comment">// 以 &lt;rootDir&gt;/src 这个目录做为根目录来搜索测试文件（模块）</span></span><br><span class="line">    roots: [</span><br><span class="line">        <span class="string">'&lt;rootDir&gt;/src'</span>,</span><br><span class="line">    ],</span><br><span class="line">    <span class="comment">// 在测试环境准备好之后且每个测试文件执行之前运行下述文件</span></span><br><span class="line">    setupFilesAfterEnv: [</span><br><span class="line">        <span class="string">'&lt;rootDir&gt;/test/index.ts'</span>,</span><br><span class="line">    ],</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 测试文件模块之间的引用应该是自己实现了一套类似于 Node 的引用机制</span></span><br><span class="line">    <span class="comment">// 不过自己可以配置，下面 module 开头的都是配置这个的，都用例子来说明</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 例如，require('./a') 语句会先找 `a.ts`，找不到找 `a.js`</span></span><br><span class="line">    moduleFileExtensions: [</span><br><span class="line">        <span class="string">'ts'</span>,</span><br><span class="line">        <span class="string">'js'</span>,</span><br><span class="line">    ],</span><br><span class="line">    <span class="comment">// 例如，require('a') 语句会递归往上层的 node_modules 中寻找 a 模块</span></span><br><span class="line">    moduleDirectories: [</span><br><span class="line">        <span class="string">'node_modules'</span>,</span><br><span class="line">    ],</span><br><span class="line">    <span class="comment">// 例如，require('@/a.js') 会解析成 require('&lt;rootDir&gt;/src/a.js')</span></span><br><span class="line">    moduleNameMapper: &#123;</span><br><span class="line">        <span class="string">'^@/test$'</span>: <span class="string">'&lt;rootDir&gt;/test/index.js'</span>,</span><br><span class="line">        <span class="string">'^@/test/(.*)$'</span>: <span class="string">'&lt;rootDir&gt;/test/$1'</span>,</span><br><span class="line">        <span class="string">'^@/(.*)$'</span>: <span class="string">'&lt;rootDir&gt;/src/$1'</span>,</span><br><span class="line">        <span class="string">'\\.(css|sass|scss)$'</span>: <span class="string">'identity-obj-proxy'</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 转译下列模块为 Jest 能识别的代码</span></span><br><span class="line">    transform: &#123;</span><br><span class="line">        <span class="string">'\\.(styl)$'</span>: <span class="string">'jest-css-modules'</span>,</span><br><span class="line">        <span class="string">'\\.(sass|scss)$'</span>: <span class="string">'jest-css-modules'</span>,</span><br><span class="line">        <span class="string">'.*\\.(j|t)s$'</span>: <span class="string">'ts-jest'</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 收集这些文件的测试覆盖率</span></span><br><span class="line">    collectCoverageFrom: [</span><br><span class="line">        <span class="string">'src/**/*.&#123;js,ts,tsx&#125;'</span>,</span><br><span class="line">        <span class="string">'!**/*.d.ts'</span>,</span><br><span class="line">    ],</span><br><span class="line">    transformIgnorePatterns: [</span><br><span class="line">        <span class="string">'node_modules/(?!vue-router)'</span>,</span><br><span class="line">    ],</span><br><span class="line">    snapshotSerializers: [</span><br><span class="line">        <span class="string">'jest-serializer-html'</span>,</span><br><span class="line">    ],</span><br><span class="line">    <span class="comment">// 从下列文件中寻找测试文件</span></span><br><span class="line">    testMatch: [</span><br><span class="line">        <span class="comment">// Default</span></span><br><span class="line">        <span class="string">'**/test/**/*.js'</span>,</span><br><span class="line">        <span class="string">'**/__tests__/**/*.spec.js'</span>,</span><br><span class="line">        <span class="string">'**/__tests__/**/*.spec.ts'</span>,</span><br><span class="line">    ],</span><br><span class="line">    <span class="comment">// 将 `ts-jest` 的配置注入到运行时的全局变量中</span></span><br><span class="line">    globals: &#123;</span><br><span class="line">        <span class="string">'ts-jest'</span>: &#123;</span><br><span class="line">            <span class="comment">// 是否使用 babel 配置来转译</span></span><br><span class="line">            babelConfig: <span class="literal">true</span>,</span><br><span class="line">            <span class="comment">// 编译 Typescript 所依赖的配置</span></span><br><span class="line">            tsConfig: <span class="string">'&lt;rootDir&gt;/tsconfig.test.json'</span>,</span><br><span class="line">            <span class="comment">// 是否启用报告诊断，这里是不启用</span></span><br><span class="line">            diagnostics: <span class="literal">false</span>,</span><br><span class="line">        &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// Jest 文档中无此配置，应该已经过时了</span></span><br><span class="line">    name: <span class="string">'Vuetify'</span>,</span><br><span class="line">    <span class="comment">// 测试文件名旁边显示的标识</span></span><br><span class="line">    displayName: <span class="string">'Vuetify'</span>,</span><br><span class="line">    <span class="comment">// 在测试环境准备好之前且每个测试文件执行之前运行下述模块</span></span><br><span class="line">    setupFiles: [</span><br><span class="line">        <span class="string">'jest-canvas-mock'</span></span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*测试DOM元素，UI测试大部分由底层的EViewUI库来实现，</span></span><br><span class="line"><span class="comment">剩下少部分UI测试使用DOM元素测试即可。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">"@testing-library/jest-dom/extend-expect"</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; render, cleanup &#125; <span class="keyword">from</span> <span class="string">'@testing-library/react'</span>;</span><br><span class="line"><span class="keyword">import</span> TestElements <span class="keyword">from</span> <span class="string">'../components/TestElements'</span></span><br><span class="line"></span><br><span class="line">afterEach(cleanup);</span><br><span class="line"></span><br><span class="line">  it(<span class="string">'should equal to 0'</span>, () =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; getByTestId &#125; = render(<span class="xml"><span class="tag">&lt;<span class="name">TestElements</span> /&gt;</span></span>); </span><br><span class="line">    expect(getByTestId(<span class="string">'counter'</span>)).toHaveTextContent(<span class="number">0</span>)</span><br><span class="line">   &#125;);</span><br><span class="line"></span><br><span class="line">   it(<span class="string">'should be enabled'</span>, () =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; getByTestId &#125; = render(<span class="xml"><span class="tag">&lt;<span class="name">TestElements</span> /&gt;</span></span>);</span><br><span class="line">    expect(getByTestId(<span class="string">'button-up'</span>)).not.toHaveAttribute(<span class="string">'disabled'</span>)</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">  it(<span class="string">'should be disabled'</span>, () =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; getByTestId &#125; = render(<span class="xml"><span class="tag">&lt;<span class="name">TestElements</span> /&gt;</span></span>); </span><br><span class="line">    expect(getByTestId(<span class="string">'button-down'</span>)).toBeDisabled()</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 测试事件响应，使用fireEvent触发事件，再对之后产生的结果进行断言，</span></span><br><span class="line"><span class="comment">判断事件响应后的逻辑是否正确。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">"@testing-library/jest-dom/extend-expect"</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; render, cleanup, fireEvent &#125; <span class="keyword">from</span> <span class="string">'@testing-library/react'</span>;</span><br><span class="line"><span class="keyword">import</span> TestEvents <span class="keyword">from</span> <span class="string">'../components/TestEvents'</span></span><br><span class="line"></span><br><span class="line">  afterEach(cleanup);</span><br><span class="line"></span><br><span class="line">  it(<span class="string">'increments counter'</span>, () =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; getByTestId &#125; = render(<span class="xml"><span class="tag">&lt;<span class="name">TestEvents</span> /&gt;</span></span>); </span><br><span class="line"></span><br><span class="line">    fireEvent.click(getByTestId(<span class="string">'button-up'</span>))</span><br><span class="line"></span><br><span class="line">    expect(getByTestId(<span class="string">'counter'</span>)).toHaveTextContent(<span class="string">'1'</span>)</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">  it(<span class="string">'decrements counter'</span>, () =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; getByTestId &#125; = render(<span class="xml"><span class="tag">&lt;<span class="name">TestEvents</span> /&gt;</span></span>); </span><br><span class="line"></span><br><span class="line">    fireEvent.click(getByTestId(<span class="string">'button-down'</span>))</span><br><span class="line"></span><br><span class="line">    expect(getByTestId(<span class="string">'counter'</span>)).toHaveTextContent(<span class="string">'-1'</span>)</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">/*异步事件的处理，对于异步事件，使用await进行处理操作 */</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">"@testing-library/jest-dom/extend-expect"</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; render, cleanup, fireEvent, waitForElement &#125; <span class="keyword">from</span> <span class="string">'@testing-library/react'</span>;</span><br><span class="line"><span class="keyword">import</span> TestAsync <span class="keyword">from</span> <span class="string">'../components/TestAsync'</span></span><br><span class="line"></span><br><span class="line">afterEach(cleanup);</span><br><span class="line"></span><br><span class="line">  it(<span class="string">'increments counter after 0.5s'</span>, <span class="keyword">async</span> () =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; getByTestId, getByText &#125; = render(<span class="xml"><span class="tag">&lt;<span class="name">TestAsync</span> /&gt;</span></span>); </span><br><span class="line"></span><br><span class="line">    fireEvent.click(getByTestId(<span class="string">'button-up'</span>))</span><br><span class="line"></span><br><span class="line">    <span class="keyword">const</span> counter = <span class="keyword">await</span> waitForElement(<span class="function"><span class="params">()</span> =&gt;</span> getByText(<span class="string">'1'</span>)) </span><br><span class="line"></span><br><span class="line">    expect(counter).toHaveTextContent(<span class="string">'1'</span>)</span><br><span class="line"></span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">/*后续的Redux、React Context、React Router、HTTP请求的测试由于时间原因还未进行实践</span></span><br><span class="line"><span class="comment">但基本的测试逻辑已经有了参考。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">"@testing-library/jest-dom/extend-expect"</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; createStore &#125; <span class="keyword">from</span> <span class="string">'redux'</span></span><br><span class="line"><span class="keyword">import</span> &#123; Provider &#125; <span class="keyword">from</span> <span class="string">'react-redux'</span></span><br><span class="line"><span class="keyword">import</span> &#123; render, cleanup, fireEvent &#125; <span class="keyword">from</span> <span class="string">'@testing-library/react'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; initialState, reducer &#125; <span class="keyword">from</span> <span class="string">'../store/reducer'</span></span><br><span class="line"><span class="keyword">import</span> TestRedux <span class="keyword">from</span> <span class="string">'../components/TestRedux'</span></span><br><span class="line"><span class="comment">/*后续的Redux、React Context、React Router、HTTP请求的测试由于时间原因还未进行实践</span></span><br><span class="line"><span class="comment">但基本的测试逻辑已经有了参考。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="comment">//Redux测试参考</span></span><br><span class="line"><span class="keyword">const</span> renderWithRedux = (</span><br><span class="line">  component,</span><br><span class="line">  &#123; initialState, store = createStore(reducer, initialState) &#125; = &#123;&#125;</span><br><span class="line">) =&gt; &#123;</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    ...render(<span class="xml"><span class="tag">&lt;<span class="name">Provider</span> <span class="attr">store</span>=<span class="string">&#123;store&#125;</span>&gt;</span>&#123;component&#125;<span class="tag">&lt;/<span class="name">Provider</span>&gt;</span></span>),</span><br><span class="line">    store,</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"> afterEach(cleanup);</span><br><span class="line"></span><br><span class="line">it(<span class="string">'checks initial state is equal to 0'</span>, () =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; getByTestId &#125; = renderWithRedux(<span class="xml"><span class="tag">&lt;<span class="name">TestRedux</span> /&gt;</span></span>)</span><br><span class="line">    expect(getByTestId(<span class="string">'counter'</span>)).toHaveTextContent(<span class="string">'0'</span>)</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  it(<span class="string">'increments the counter through redux'</span>, () =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; getByTestId &#125; = renderWithRedux(<span class="xml"><span class="tag">&lt;<span class="name">TestRedux</span> /&gt;</span></span>, </span><br><span class="line">      &#123;<span class="attr">initialState</span>: &#123;<span class="attr">count</span>: <span class="number">5</span>&#125;</span><br><span class="line">  &#125;)</span><br><span class="line">    fireEvent.click(getByTestId(<span class="string">'button-up'</span>))</span><br><span class="line">    expect(getByTestId(<span class="string">'counter'</span>)).toHaveTextContent(<span class="string">'6'</span>)</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  it(<span class="string">'decrements the counter through redux'</span>, () =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; getByTestId&#125; = renderWithRedux(<span class="xml"><span class="tag">&lt;<span class="name">TestRedux</span> /&gt;</span></span>, &#123;</span><br><span class="line">      initialState: &#123; <span class="attr">count</span>: <span class="number">100</span> &#125;,</span><br><span class="line">    &#125;)</span><br><span class="line">    fireEvent.click(getByTestId(<span class="string">'button-down'</span>))</span><br><span class="line">    expect(getByTestId(<span class="string">'counter'</span>)).toHaveTextContent(<span class="string">'99'</span>)</span><br><span class="line">  &#125;)</span><br><span class="line"><span class="comment">//React Context测试参考</span></span><br><span class="line">  <span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span></span><br><span class="line">  <span class="keyword">import</span> <span class="string">"@testing-library/jest-dom/extend-expect"</span>;</span><br><span class="line">  <span class="keyword">import</span> &#123; render, cleanup,  fireEvent &#125; <span class="keyword">from</span> <span class="string">'@testing-library/react'</span></span><br><span class="line">  <span class="keyword">import</span> CounterProvider, &#123; CounterContext, Counter &#125; <span class="keyword">from</span> <span class="string">'../components/TestContext'</span></span><br><span class="line">  <span class="keyword">const</span> renderWithContext = (</span><br><span class="line">    component) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      ...render(</span><br><span class="line">          &lt;CounterProvider value=&#123;CounterContext&#125;&gt;</span><br><span class="line">              &#123;component&#125;</span><br><span class="line">          &lt;<span class="regexp">/CounterProvider&gt;)</span></span><br><span class="line"><span class="regexp">    &#125;</span></span><br><span class="line"><span class="regexp">  &#125;</span></span><br><span class="line"><span class="regexp">  afterEach(cleanup);</span></span><br><span class="line"><span class="regexp">  it('checks if initial state is equal to 0', () =&gt; &#123;</span></span><br><span class="line"><span class="regexp">      const &#123; getByTestId &#125; = renderWithContext(&lt;Counter /</span>&gt;)</span><br><span class="line">      expect(getByTestId(<span class="string">'counter'</span>)).toHaveTextContent(<span class="string">'0'</span>)</span><br><span class="line">  &#125;)</span><br><span class="line">  </span><br><span class="line">  it(<span class="string">'increments the counter'</span>, () =&gt; &#123;</span><br><span class="line">      <span class="keyword">const</span> &#123; getByTestId &#125; = renderWithContext(<span class="xml"><span class="tag">&lt;<span class="name">Counter</span> /&gt;</span></span>)</span><br><span class="line">  </span><br><span class="line">      fireEvent.click(getByTestId(<span class="string">'button-up'</span>))</span><br><span class="line">      expect(getByTestId(<span class="string">'counter'</span>)).toHaveTextContent(<span class="string">'1'</span>)</span><br><span class="line">    &#125;)</span><br><span class="line">  </span><br><span class="line">    it(<span class="string">'decrements the counter'</span>, () =&gt; &#123;</span><br><span class="line">      <span class="keyword">const</span> &#123; getByTestId&#125; = renderWithContext(<span class="xml"><span class="tag">&lt;<span class="name">Counter</span> /&gt;</span></span>)</span><br><span class="line">  </span><br><span class="line">      fireEvent.click(getByTestId(<span class="string">'button-down'</span>))</span><br><span class="line">      expect(getByTestId(<span class="string">'counter'</span>)).toHaveTextContent(<span class="string">'-1'</span>)</span><br><span class="line">    &#125;);</span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">"@testing-library/jest-dom/extend-expect"</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; Router &#125; <span class="keyword">from</span> <span class="string">'react-router-dom'</span></span><br><span class="line"><span class="keyword">import</span> &#123; render, fireEvent &#125; <span class="keyword">from</span> <span class="string">'@testing-library/react'</span></span><br><span class="line"><span class="keyword">import</span> &#123; createMemoryHistory &#125; <span class="keyword">from</span> <span class="string">'history'</span></span><br><span class="line"><span class="keyword">import</span> TestRouter <span class="keyword">from</span> <span class="string">'../components/TestRouter'</span></span><br><span class="line"><span class="comment">//ReactRouter测试参考</span></span><br><span class="line"><span class="keyword">const</span> renderWithRouter = <span class="function">(<span class="params">component</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> history = createMemoryHistory()</span><br><span class="line">    <span class="keyword">return</span> &#123; </span><br><span class="line">    ...render (</span><br><span class="line">    &lt;Router history=&#123;history&#125;&gt;</span><br><span class="line">        &#123;component&#125;</span><br><span class="line">    &lt;<span class="regexp">/Router&gt;</span></span><br><span class="line"><span class="regexp">    )</span></span><br><span class="line"><span class="regexp">  &#125;</span></span><br><span class="line"><span class="regexp">&#125;</span></span><br><span class="line"><span class="regexp">it('should render the home page', () =&gt; &#123;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">  const &#123; container, getByTestId &#125; = renderWithRouter(&lt;TestRouter /</span>&gt;) </span><br><span class="line">  <span class="keyword">const</span> navbar = getByTestId(<span class="string">'navbar'</span>)</span><br><span class="line">  <span class="keyword">const</span> link = getByTestId(<span class="string">'home-link'</span>)</span><br><span class="line"></span><br><span class="line">  expect(container.innerHTML).toMatch(<span class="string">'Home page'</span>)</span><br><span class="line">  expect(navbar).toContainElement(link)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">it(<span class="string">'should navigate to the about page'</span>, ()=&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; container, getByTestId &#125; = renderWithRouter(<span class="xml"><span class="tag">&lt;<span class="name">TestRouter</span> /&gt;</span></span>) </span><br><span class="line"></span><br><span class="line">    fireEvent.click(getByTestId(<span class="string">'about-link'</span>))</span><br><span class="line"></span><br><span class="line">    expect(container.innerHTML).toMatch(<span class="string">'About page'</span>)</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  it(<span class="string">'should navigate to the contact page with the params'</span>, ()=&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; container, getByTestId &#125; = renderWithRouter(<span class="xml"><span class="tag">&lt;<span class="name">TestRouter</span> /&gt;</span></span>) </span><br><span class="line"></span><br><span class="line">    fireEvent.click(getByTestId(<span class="string">'contact-link'</span>))</span><br><span class="line"></span><br><span class="line">    expect(container.innerHTML).toMatch(<span class="string">'John Doe'</span>)</span><br><span class="line">  &#125;)</span><br><span class="line"><span class="comment">//HTTP请求测试参考</span></span><br><span class="line">  <span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span></span><br><span class="line">  <span class="keyword">import</span> <span class="string">"@testing-library/jest-dom/extend-expect"</span>;</span><br><span class="line">  <span class="keyword">import</span> &#123; render, waitForElement, fireEvent &#125; <span class="keyword">from</span> <span class="string">'@testing-library/react'</span></span><br><span class="line">  <span class="keyword">import</span> axiosMock <span class="keyword">from</span> <span class="string">'axios'</span></span><br><span class="line">  <span class="keyword">import</span> TestAxios <span class="keyword">from</span> <span class="string">'../components/TestAxios'</span></span><br><span class="line"></span><br><span class="line">  jest.mock(<span class="string">'axios'</span>)</span><br><span class="line">  </span><br><span class="line">  it(<span class="string">'should display a loading text'</span>, () =&gt; &#123;</span><br><span class="line">  </span><br><span class="line">   <span class="keyword">const</span> &#123; getByTestId &#125; = render(<span class="xml"><span class="tag">&lt;<span class="name">TestAxios</span> /&gt;</span></span>)</span><br><span class="line">  </span><br><span class="line">    expect(getByTestId(<span class="string">'loading'</span>)).toHaveTextContent(<span class="string">'Loading...'</span>)</span><br><span class="line">  &#125;)</span><br><span class="line">  </span><br><span class="line">  it(<span class="string">'should load and display the data'</span>, <span class="keyword">async</span> () =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> url = <span class="string">'/greeting'</span></span><br><span class="line">    <span class="keyword">const</span> &#123; getByTestId &#125; = render(<span class="xml"><span class="tag">&lt;<span class="name">TestAxios</span> <span class="attr">url</span>=<span class="string">&#123;url&#125;</span> /&gt;</span></span>)</span><br><span class="line">  </span><br><span class="line">    axiosMock.get.mockResolvedValueOnce(&#123;</span><br><span class="line">      data: &#123; <span class="attr">greeting</span>: <span class="string">'hello there'</span> &#125;,</span><br><span class="line">    &#125;)</span><br><span class="line">  </span><br><span class="line">    fireEvent.click(getByTestId(<span class="string">'fetch-data'</span>))</span><br><span class="line">  </span><br><span class="line">    <span class="keyword">const</span> greetingData = <span class="keyword">await</span> waitForElement(<span class="function"><span class="params">()</span> =&gt;</span> getByTestId(<span class="string">'show-data'</span>))</span><br><span class="line">  </span><br><span class="line">    expect(axiosMock.get).toHaveBeenCalledTimes(<span class="number">1</span>)</span><br><span class="line">    expect(axiosMock.get).toHaveBeenCalledWith(url)</span><br><span class="line">    expect(greetingData).toHaveTextContent(<span class="string">'hello there'</span>)</span><br><span class="line">  &#125;)</span><br></pre></td></tr></table></figure>



<h3 id="4、重构整体集成测试"><a href="#4、重构整体集成测试" class="headerlink" title="4、重构整体集成测试"></a>4、重构整体集成测试</h3>
    </div>
    
    
    
    
    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      
    </div>
        <div class="reward-container">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/alipay.png" alt="Li Yudong 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Li Yudong
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://example.com/2021/08/15/%E5%AD%A6%E6%A0%A1%E9%A1%B9%E7%9B%AE%E5%BA%94%E7%94%A8/jest%E6%B5%8B%E8%AF%95/" title="Jest测试（待整理）">http://example.com/2021/08/15/学校项目应用/jest测试/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

    
      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E5%AD%A6%E6%A0%A1%E9%A1%B9%E7%9B%AE%E5%BA%94%E7%94%A8/" rel="tag"># 学校项目应用</a>
          </div>
    
        

    
        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/08/15/%E9%9D%A2%E8%AF%95%E6%80%BB%E7%BB%93/JS%E6%89%8B%E5%86%99%E9%97%AE%E9%A2%98/" rel="prev" title="JS手写问题">
      <i class="fa fa-chevron-left"></i> JS手写问题
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/08/24/node/egg-node%E5%AE%9E%E6%88%98/" rel="next" title="数据库">
      数据库 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#项目背景"><span class="nav-number">1.</span> <span class="nav-text">项目背景</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#需求分析"><span class="nav-number">2.</span> <span class="nav-text">需求分析</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#实践步骤"><span class="nav-number">3.</span> <span class="nav-text">实践步骤</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1、-测试框架选用与依赖配置"><span class="nav-number">3.1.</span> <span class="nav-text">1、 测试框架选用与依赖配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2、-Redux与React-Intl模拟"><span class="nav-number">3.2.</span> <span class="nav-text">2、 Redux与React-Intl模拟</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3、-单元测试与UI快照生成"><span class="nav-number">3.3.</span> <span class="nav-text">3、 单元测试与UI快照生成</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4、重构整体集成测试"><span class="nav-number">3.4.</span> <span class="nav-text">4、重构整体集成测试</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Li Yudong"
      src="/images/author.jpg">
  <p class="site-author-name" itemprop="name">Li Yudong</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">95</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/tiarmor1" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;tiarmor1" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1157019137@qq.com" title="E-Mail → mailto:1157019137@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2020 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Li Yudong</span>
</div>

        








      </div>
    </footer>
  </div>

  
  
  <script color='0,0,0' opacity='0.5' zIndex='-1' count='150' src="/lib1/canvas-nest/canvas-nest.min.js"></script>
  <script src="/lib1/anime.min.js"></script>
  <script src="/lib1/velocity/velocity.min.js"></script>
  <script src="/lib1/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  

  
   <canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none;" ></canvas> 
   <script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script> 
   <script type="text/javascript" src="/js/fireworks.js"></script>
  
</body>
</html>

